<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录界面</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
    <style>
        body {
            width: 100%;
            height: 100%;
            background-size: 100% 100%;
            /* 不设置的话页面滑动时，背景会不铺满*/
            background: url(/images/back.jpg) no-repeat fixed;
        }
        #canvas {
            /*float: right;*/
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 5px;
            cursor: pointer;
            background: white;
            margin-left: 36px;
        }
        .container{
            width: 450px;
            height: 360px;
            margin-top: 130px;
            border: 1px solid #009688;
            padding-top: 30px;
            border-radius: 15px;
            background: #cfdb47;
            margin-right: 90px;
            opacity: 0.8;
        }

        .inputdiv {
            display: flex;
            border: 1px solid #D2D2D2 !important;
            background-color: #fff;
            height: 38px;
            line-height: 38px;
            padding: 0px 19px;
            border-radius: 10px;
            width: 300px;
        }

        .inputdiv2 {
            display: flex;
            border: 1px solid #D2D2D2 !important;
            background-color: #fff;
            height: 38px;
            line-height: 38px;
            padding: 0px 19px;
            border-radius: 10px;
            width: 180px;
        }

        .layui-input {
            border-style: none;
        }
    </style>
    <script type="text/javascript">

        $(function () {
            var show_num = [];
            draw(show_num);

            $("#canvas").on('click', function () {
                draw(show_num);
            });
        });

        function draw(show_num) {
            var canvas_width = $('#canvas').width();
            var canvas_height = $('#canvas').height();
            var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
            var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
            canvas.width = canvas_width;
            canvas.height = canvas_height;
            var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            var aCode = sCode.split(",");
            var aLength = aCode.length;//获取到数组的长度

            for (var i = 0; i <= 3; i++) {
                var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
                var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
                var txt = aCode[j];//得到随机的一个内容
                show_num[i] = txt.toLowerCase();
                var x = 10 + i * 20;//文字在canvas上的x坐标
                var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
                context.font = "bold 23px 微软雅黑";

                context.translate(x, y);
                context.rotate(deg);

                context.fillStyle = randomColor();
                context.fillText(txt, 0, 0);

                context.rotate(-deg);
                context.translate(-x, -y);
            }
            for (var i = 0; i <= 5; i++) { //验证码上显示线条
                context.strokeStyle = randomColor();
                context.beginPath();
                context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.stroke();
            }
            for (var i = 0; i <= 30; i++) { //验证码上显示小点
                context.strokeStyle = randomColor();
                context.beginPath();
                var x = Math.random() * canvas_width;
                var y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        }

        function randomColor() {//得到随机的颜色值
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return "rgb(" + r + "," + g + "," + b + ")";
        }
    </script>
</head>
<body>
<input id="msg" th:value="${msg}" type="hidden"/>
<input id="rMsg" th:value="${rMsg}" type="hidden"/>

<div class="layui-container container">
    <div style="text-align: center;margin-bottom: 20px;font-size: 30px ;color: white">用户登录</div>
    <form class="layui-form" action="/account/login" method="post" style="padding-left: 10px">
        <div class="layui-form-item">
            <div class="inputdiv">
                <i class="layui-icon layui-icon-username"></i>
                <input type="text" name="userName" lay-verify="userName" autocomplete="off" placeholder="请输入用户名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="inputdiv">
                <i class="layui-icon layui-icon-password"></i>
                <input type="password" name="password" lay-verify="password" autocomplete="off" placeholder="请输入密码"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <div class="inputdiv2">
                    <i class="layui-icon layui-icon-vercode"></i>
                    <input type="text" value="" placeholder="请输入验证码" class="input-val layui-input" id="imgCode"
                           name="imgCode" lay-verify="imgCode">
                </div>
            </div>
            <div class="layui-input-inline">
                <canvas id="canvas" width="100px" height="35px"></canvas>
            </div>

        </div>
        <div class="layui-form-item" pane="" style="margin-left: 10px">
            <div class="layui-input-block">
                <input type="radio" name="type" value="admin" title="管理员" checked="">
                <input type="radio" name="type" value="user" title="用户">
            </div>
        </div>


        <div class="layui-form-item" style="text-align:center;">
            <button class="layui-btn" lay-submit="" lay-filter="demo2" id="btn" style="margin-left:auto; margin-right:auto">登录</button>
            <button class="layui-btn" type="button" onclick="register()" style="margin-left:auto; margin-right:auto">注册</button>
        </div>
    </form>

</div>
<script src="/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.use(['form'], function () {
        var form = layui.form;
        var show_num = [];
        draw(show_num);
        //自定义验证规则
        form.verify({
            userName: function (value) {
                if (value.length == 0) {
                    return '用户名不能为空';
                }
            }
            , password: [/(.+){6,12}$/, '密码必须6到12位']
            , imgCode: function () {
                var val = $(".input-val").val().toLowerCase();
                var num = show_num.join("");
                if (val == '') {
                    return '请输入验证码！';
                } else if (val == num) {
                    $(".input-val").val('');
                    draw(show_num);

                } else {
                    // alert('验证码错误！请重新输入！');
                    $(".input-val").val('');
                    draw(show_num);
                    return '验证码错误！请重新输入！';
                }
            }
        });

        var msg=$('#msg').val();
        var rMsg=$('#rMsg').val();
        console.log("msg:"+msg);
        if(msg!=null&&msg!==''){
            if(msg==='error'){
                layer.msg('用户名或密码错误');
            }else if(msg==='success'){
                layer.msg('登录成功', {
                    icon: 1
                    // time: 3000
                });
            }
        }

        if(rMsg!=null&&rMsg!==''){
            if(rMsg==='error'){
                layer.msg('用户已存在!请重新注册!');
            }else if(rMsg==='success'){
                layer.msg('注册成功，请登录！', {
                    icon: 1,
                    time: 3000
                });
            }
        }


    });

    function register() {
        window.location.href='register.html';
    }

</script>
</body>
</html>